<template>
	<div class="login_bg" :style="{'background-image': `url(`+this.backImag+`)`,'background-size': 'cover'}">
		<div class="login_main">
			<div class="login_config">

				<el-button :icon="config.dark?'el-icon-sunny':'el-icon-moon'" circle type="info" @click="configDark"></el-button>
				<el-dropdown trigger="click" placement="bottom-end" @command="configLang" v-if="langFlag=='1'">
					<el-button circle>
						<el-icon>
							<sc-icon-change-lang/>
						</el-icon>
					</el-button>
					<template #dropdown>
						<el-dropdown-menu>
							<el-dropdown-item v-for="item in lang" :key="item.value" :command="item" :class="{'selected':config.lang==item.value}">{{item.name}}</el-dropdown-item>
						</el-dropdown-menu>
					</template>
				</el-dropdown>
			</div>
			<!-- <div class="login_adv__bottom">
					<div style="text-align: center;">
						<span v-if="tecSupport!=''" style="margin-left: 20px;float: left">技术支持:{{tecSupport}}</span>
						<span v-if="recordNumber!=''" v-html="recordNumber"></span>
						<span style="margin-right: 20px;float: right">© {{APP_NAME}} {{VERSION}}</span>
					</div>
			</div> -->
			<div class="login-form">
				<!-- <div class="login-header">
					<div class="logo">
						<img :src="'img/syslogo.png'" />
						<label :style="`color:`+appTitleColor">{{sysTitle}}</label>
					</div>
				</div> -->
				<password-form :verifyCode="verifyCode" :find-pass="findPass" :rem-user-pass="remUserPass" v-if="isShow" :app-color="appColor"></password-form>
			</div>
		</div>
	</div>
</template>

<script>
	import config from "@/config"
	import passwordForm from './components/passwordForm'
	import phoneForm from './components/phoneForm'
	import {setSystemGrey} from "@/utils/util";
	export default {

		components: {
			passwordForm,
			phoneForm,
		},
		data() {
			return {
				apiUrl:config.API_URL,
				encyCode:this.$route.query.encyCode,
				verifyCode:'',
				tecSupport:'',
				recordNumber:'',
				sysTitle:'',
				appColor:'',
				appTitleColor:'',
				APP_NAME:'',
				VERSION:'',
				langFlag:'0',
				config: {
					lang: this.$TOOL.data.get('APP_LANG') || this.$CONFIG.LANG,
					dark: this.$TOOL.data.get('APP_DARK') || false
				},
				lang: [
					{
						name: '简体中文',
						value: 'zh-cn',
					},
					{
						name: 'English',
						value: 'en',
					}
				],
				WechatLoginCode: "",
				isWechatLoginResult: false,
				backImag:'',
				backImagArr:[],
				timer:null,
				findPass:'0',
				remUserPass:'0',
				isShow:false
			}
		},
		watch:{
			'config.dark'(val){
				if(val){
					document.documentElement.classList.add("dark")
					this.$TOOL.data.set("APP_DARK", val)
				}else{
					document.documentElement.classList.remove("dark")
					this.$TOOL.data.remove("APP_DARK")
				}
			},
			'config.lang'(val){
				this.$i18n.locale = val
				this.$TOOL.data.set("APP_LANG", val)
			},

		},
		created: function() {
			this.getSysInterfaceForLogin();
			this.$TOOL.cookie.remove("TOKEN")
			this.$TOOL.data.remove("USER_INFO")
			this.$TOOL.data.remove("MENU")
			this.$TOOL.data.remove("PERMISSIONS")
			this.$TOOL.data.remove("grid")
			this.$store.commit("clearViewTags")
			this.$store.commit("clearKeepLive")
			this.$store.commit("clearIframeList")
			// 每次进入界面时，先清除之前的所有定时器，然后启动新的定时器
			clearInterval(this.timer)
			this.timer = null
		},
		methods: {
			async getSysInterfaceForLogin(){
				let res = await this.$API.system.sysInterface.getSysInterfaceForLogin.get();
				if(res.code==200)
				{
					this.$TOOL.data.set("ATTACH_HOST", res.data.pageOfficeHost)
					this.$TOOL.data.set("OFFICE_TYPE", res.data.officetype)
					this.$TOOL.data.set("OFD_TYPE",res.data.ofdtype);
					this.$TOOL.data.set("OPEN_ON_LINE_FLAG",res.data.openOnLineFlag);
					this.$TOOL.data.set("SYSTEM_GREY",res.data.systemGrey);
					this.$TOOL.data.set("ENCRYPTION_TYPE",res.data.encryptionType);
					this.$TOOL.data.set("PUBLIC_KEY",res.data.publicKey);
					this.appTitleColor = res.data.appTitleColor;
					this.appColor = res.data.appColor;
					let greyJson = res.data.systemGrey;
					if(greyJson.greyStatus==='1')
					{
						setSystemGrey(greyJson.greyEndTime);
					}else{
						window.localStorage.removeItem("GREY_END_TIME");
					}
					this.remUserPass = res.data.remUserPass;
					this.findPass = res.data.findPass;
					if(this.remUserPass=='0')
					{
						this.$TOOL.cookie.remove("TOKEN");
						this.$TOOL.cookie.remove("AUTO_LOGIN");
						this.$TOOL.cookie.remove("ACCOUNT_ID");
						this.$TOOL.cookie.remove("PASS_WORD");
					}
					if(this.encyCode!=undefined&&this.encyCode!='')
					{
						var user = await this.$API.auth.token.post({encyCode:this.encyCode});
						if (user.code == 200) {
							this.$TOOL.cookie.set("TOKEN", user.data.token, {
								expires:24 * 60 * 60
							})
							this.$TOOL.data.set("USER_INFO", user.data.userInfo)
							this.$TOOL.data.set('APP_LANG', user.data.userInfo.language);
							this.$TOOL.data.set('APP_COLOR', user.data.userInfo.theme);
							this.$TOOL.data.set("MSG_TIME",user.data.msgtime);
						} else if (user.code == 300) {
							this.$router.replace({
								path: '/system/info'
							})
							return;
						} else if (user.code == 400) {
							this.$router.replace({
								path: '/resetPassword'
							})
							return;
						} else {
							this.islogin = false
							this.$message.warning(user.message)
							return false
						}
						//获取菜单
						console.log('66666666666666')
						var menu = await this.$API.system.menu.myMenus.get()
						if (menu.code == 200) {
							if (menu.data.menu.length == 0) {
								this.islogin = false
								this.$alert("当前用户无任何菜单权限，请联系系统管理员", "无权限访问", {
									type: 'error',
									center: true
								})
								return false
							}
							this.$TOOL.data.set("MENU", menu.data.menu)
							this.$TOOL.data.set("PERMISSIONS", [
								"list.add",
								"list.edit",
								"list.delete",
								"user.add",
								"user.edit",
								"user.delete"
							])
						} else {
							this.islogin = false
							this.$message.warning(menu.message)
							return false
						}
						this.$router.replace({
							path: '/'
						})
					}
					this.APP_NAME = res.data.APP_NAME;
					this.VERSION = res.data.VERSION;
					this.verifyCode = res.data.verifyCode;
					this.$CONFIG.APP_VER = res.data.VERSION;
					this.recordNumber = res.data.recordNumber;
					this.langFlag = res.data.langFlag;
					if(res.data.sysTitle!=undefined&&res.data.sysTitle!='')
					{
						this.sysTitle = res.data.sysTitle;
						this.$CONFIG.APP_NAME = res.data.sysTitle;
					}else {
						this.sysTitle=this.$CONFIG.APP_NAME;
					}
					this.$TOOL.data.set("APP_NAME",this.sysTitle)
					document.title = "登录 - "+this.$CONFIG.APP_NAME;
					config.APP_NAME = this.$CONFIG.APP_NAME;
					if(res.data.backgroundImg!=undefined&&res.data.backgroundImg!='')
					{
						let arr =res.data.backgroundImg.split(",");
						arr.forEach(item=>{
							this.backImagArr.push(item)
						})
						this.backImag = this.backImagArr[0];
					}else {
						// this.backImagArr=['img/backgrounds/bg1.jpg','img/backgrounds/bg2.jpg','img/backgrounds/bg3.jpg','img/backgrounds/bg4.jpg'];
						// this.backImagArr=['img/backgrounds/bg1.jpg'];
						// this.backImag = this.backImagArr[0];
					}
					this.tecSupport = res.data.tecSupport;
					this.setTimer();
					this.isShow = true;
				}
			},
			configDark(){
				this.config.dark = this.config.dark ? false : true
			},
			configLang(command){
				this.config.lang = command.value
			},
			setTimer() {
				if(this.timer == null) {
					let count=1;
					let length = this.backImagArr.length;
					this.timer = setInterval( () => {
						this.backImag=this.backImagArr[(count%length)];
						count = count+1;
					}, 10000)
				}
			}
		}
	}
</script>
<style scoped>
	.login_bg {width: 100%;height: 100%;background: #fff;display: flex;}
	.login_adv {width: 100%;background-color: #555;background-size: cover;background-position: center center;background-repeat: no-repeat;position: relative;}
	.login_adv__title {color: #fff;margin: 40px;position: absolute;top:0px;left:0px;right: 0px;z-index: 2;}
	.login_adv__title h2 {font-size: 40px;}
	.login_adv__title h4 {font-size: 18px;margin-top: 10px;font-weight: normal;}
	.login_adv__title p {font-size: 14px;margin-top:10px;line-height: 1.8;color: rgba(255,255,255,0.6);}
	.login_adv__title div {margin-top: 10px;display: flex;align-items: center;}
	.login_adv__title div span {margin-right: 15px;}
	.login_adv__title div i {font-size: 40px;}
	.login_adv__title div i.add {font-size: 20px;color: rgba(255,255,255,0.6);}
	.login_adv__bottom {position: absolute;left:0px;right: 0px;bottom: 0px;color: #fff;padding: 40px;background-image:linear-gradient(transparent, #000);z-index: 3;text-align: right;opacity: 0.6;}

	.login_main {flex: 1;overflow: auto;display:flex;color:white;}
	.login-form {width: 400px;margin: auto;padding:10px 0;}
	.login-header {margin-bottom: 40px;}
	.login-header .logo {display: flex;align-items: center;}
	.login-header .logo img {height: 40px;vertical-align: bottom;margin-right: 10px;}
	.login-header .logo label {font-size: 26px;font-weight: bold;}
	.login-form {}

	.login-form:deep(.login-forgot) {text-align: right;}
	.login-form:deep(.login-forgot) a {color: var(--el-color-primary);}
	.login-form:deep(.login-forgot) a:hover {color: var(--el-color-primary-light-3);}
	.login-form:deep(.login-reg) {font-size: 14px;color: var(--el-text-color-primary);}
	.login-form:deep(.login-reg) a {color: var(--el-color-primary);}
	.login-form:deep(.login-reg) a:hover {color: var(--el-color-primary-light-3);}

	.login_config {position: absolute;top:20px;right: 20px;}

	.login-form:deep(.login-msg-yzm) {display: flex;width: 100%;}
	.login-form:deep(.login-msg-yzm) .el-button {margin-left: 10px;--el-button-size:42px;}

	@media (max-width: 1200px){
		.login-form {width: 340px;}
	}
	@media (max-width: 1000px){
		.login_main {display: block;}
		.login_main .login_config {position: static;padding:20px 20px 0 20px;text-align: right;}
		.login-form {width:100%;padding:20px 40px;margin-top: 25%;}
	}
</style>
